import sys
import os
import shutil
from datetime import datetime
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFileDialog, QVBoxLayout, QLabel, QMessageBox

class RenameFileApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('文件重命名工具')
        self.setGeometry(300, 300, 400, 150)

        self.label = QLabel('请选择要重命名的文件', self)
        self.btn = QPushButton('选择文件', self)
        self.btn.clicked.connect(self.openFileDialog)

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.btn)
        self.setLayout(layout)

    def openFileDialog(self):
        file_path, _ = QFileDialog.getOpenFileName(self, '选择文件', '', '所有文件 (*)')
        if file_path:
            self.renameFile(file_path)

    def renameFile(self, file_path):
        dir_name = os.path.dirname(file_path)
        ext = os.path.splitext(file_path)[1]
        today_str = datetime.now().strftime('%Y-%m-%d')
        new_name = f"{today_str}{ext}"
        new_path = os.path.join(dir_name, new_name)

        # 如果新文件已存在，避免覆盖
        count = 1
        while os.path.exists(new_path):
            new_name = f"{today_str}_{count}{ext}"
            new_path = os.path.join(dir_name, new_name)
            count += 1

        shutil.copy2(file_path, new_path)
        QMessageBox.information(self, "成功", f"文件已重命名为：{new_name}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = RenameFileApp()
    ex.show()
    sys.exit(app.exec_())